﻿//JumbotCms_4.1 Final
using System;
using System.Data;
using System.Web;
using System.Web.UI.WebControls;
using Jumbot.Common;
using JumbotCms.Common;
namespace JumbotCms.Modules.Version.AdminChart
{
    public partial class _ajax : JumbotCms.API.AdminCenter
    {
        private string _operType = string.Empty;
        private string _response = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            ChannelId = Str2Str(q("ccid"));
            this._operType = q("oper");
            Admin_Load("", "json", true);
            switch (this._operType)
            {

                case "ajaxGetList":
                    ajaxGetList();
                    break;
                default:
                    DefaultResponse();
                    break;
            }
            Response.Write(this._response);
        }

        private void DefaultResponse()
        {
            this._response = JsonResult(0, "未知操作");
        }
        private void ajaxGetList()
        {
            int _y = Str2Int(q("y"), 0);
            int _m = Str2Int(q("m"), 0);
            int _t = Str2Int(q("t"), 1);
            string returnYM = "";
            string whereStr1 = "a.ChannelId=" + ChannelId;
            string whereStr2 = "ChannelId=" + ChannelId;
            if (_y < 2000 || _y > DateTime.Now.Year) _y = 1999;
            if (_y >= 2000)
            {
                returnYM += _y + " 年度 ";
                if (_m > 0 && _m < 13) //统计月份
                {
                    returnYM += _m + " 月份 ";
                    if (DBType == "0")
                    {
                        whereStr1 += " AND datediff('m',A.AddDate,'" + (_y + "-" + _m + "-1") + "')=0";
                        whereStr2 += " AND datediff('m',AddDate,'" + (_y + "-" + _m + "-1") + "')=0";
                    }
                    else
                    {
                        whereStr1 += " AND datediff(m,A.AddDate,'" + (_y + "-" + _m + "-1") + "')=0";
                        whereStr2 += " AND datediff(m,AddDate,'" + (_y + "-" + _m + "-1") + "')=0";
                    }
                }
                else//只统计年
                {
                    if (DBType == "0")
                    {
                        whereStr1 += " AND (A.AddDate>=#" + (_y + "-1-1") + "# AND A.AddDate<#" + ((_y + 1) + "-1-1") + "#)";
                        whereStr2 += " AND (AddDate>=#" + (_y + "-1-1") + "# AND AddDate<#" + ((_y + 1) + "-1-1") + "#)";
                    }
                    else
                    {
                        whereStr1 += " AND (A.AddDate>='" + (_y + "-1-1") + "' AND A.AddDate<'" + ((_y + 1) + "-1-1") + "')";
                        whereStr2 += " AND (AddDate>='" + (_y + "-1-1") + "' AND AddDate<'" + ((_y + 1) + "-1-1") + "')";
                    }
                }
            }
            doh.Reset();
            doh.ConditionExpress = whereStr2;
            int _contentcount = doh.Count("jcms_module_version");
            doh.Reset();
            doh.ConditionExpress = whereStr2;
            int _sumword = doh.Sum("jcms_module_version", "WordCount");
            doh.Reset();
            if (_t != 2)
                doh.SqlCmd = "SELECT a.ID as classid,a.Title as classname,(SELECT count(id) FROM [jcms_module_version] where [ClassID] in (SELECT ID FROM [jcms_normal_class] WHERE [IsOut]=0 AND [Code] Like (SELECT Code FROM [jcms_normal_class] WHERE [IsOut]=0 AND [Id]=a.id)+'%') AND " + whereStr2 + ") as classcontentcount,(SELECT sum(wordcount) FROM [jcms_module_version] where [ClassID] in (SELECT ID FROM [jcms_normal_class] WHERE [IsOut]=0 AND [Code] Like (SELECT Code FROM [jcms_normal_class] WHERE [IsOut]=0 AND [Id]=a.id)+'%') AND " + whereStr2 + ") as classsumword FROM [jcms_normal_class] as a WHERE a.ParentId=0 AND a.ChannelId=" + ChannelId + " ORDER BY a.code";
            else
                doh.SqlCmd = "SELECT a.UserId as writerid,a.Translator as writername,(SELECT count(id) FROM [jcms_module_version] where [UserId]=a.UserId AND [Translator]=a.Translator AND " + whereStr2 + ") as writercontentcount,(SELECT sum(wordcount) FROM [jcms_module_version] where [UserId]=a.UserId AND [Translator]=a.Translator AND " + whereStr2 + ") as writersumword FROM [jcms_module_version] as a WHERE a.IsPass=1 AND " + whereStr1 + " GROUP BY a.UserId,a.Translator";
            //doh.SqlCmd = "SELECT a.ID as writerid,a.UserName as writername,(SELECT count(id) FROM [jcms_module_version] where [UserID]=a.id AND " + whereStr2 + ") as writercontentcount,(SELECT sum(wordcount) FROM [jcms_module_version] where [UserID]=a.id AND " + whereStr2 + ") as writersumword FROM [jcms_normal_user] as a WHERE a.IsWriter=1 ORDER BY a.Id";
            //this._response = doh.SqlCmd;
            DataTable dt = doh.GetDataTable();
            this._response = "{result :\"1\",returnval :\"" + returnYM + "\",contentcount :" + _contentcount + ",sumword :" + _sumword + "," + Jumbot.Common.JsonHelp.DtToSON(dt) + "}";
        }
    }
}